﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Debug = System.Diagnostics.Debug;

namespace CaoNiMaCoolPad
{
    class Folders
    {
        private const int nameLength = 88;
        private static readonly byte[] folderBeginIdentifier = { 0xda, 0x7, 0, 0, 0x4, 0, 0, 0, 0xb, 0, 0, 0, 0, 0, 0, 0, 0x3, 0, 0, 0, 0x1a, 0 };


        private readonly Func<Stream> m_getStream;
        private bool m_Inited = false;

        private readonly List<string> ids = new List<string>();
        private readonly List<string> names = new List<string>();

        public Folders(Func<Stream> getStream)
        {
            m_getStream = getStream;
        }

        public string this[string id]
        {
            get
            {
                if (m_Inited == false)
                    Init();

                int index = ids.IndexOf(id);
                if (index != -1)
                    return names[index];
                else
                    throw new KeyNotFoundException("找不到" + id + "对应的文件夹名称。");
            }
        }

        public string this[int index]
        {
            get
            {
                if (m_Inited == false)
                    Init();
                return names[index];
            }
        }

        public bool Contains(string id)
        {
            if (m_Inited == false)
                Init();
            return ids.Contains(id);
        }

        private void Init()
        {
            using (Stream stm = m_getStream())
            {
                bool found = true;
                Func<Stream, Tuple<bool, byte>> fun = s =>
                              {
                                  int r = s.ReadByte();
                                  if (r == -1)
                                  {
                                      found = false;
                                      return Tuple.Create<bool, byte>(false, 0);
                                  }
                                  else
                                      return Tuple.Create(true, (byte)r);
                              };

                MyLibrary.Algorithms.KmpMatch(stm, folderBeginIdentifier, fun);
                if (found)
                {
                    byte[] binary = new byte[stm.Length - stm.Position];
                    int read;
                    int i = 0;
                    while ((read = stm.ReadByte()) != -1)
                        binary[i++] = (byte)read;

                    i = 10;
                    do
                    {
                        ids.Add(Program.ByteArrayToString(binary, i, 4));
                        i += 4;
                        names.Add(Program.PhraseUnicode(binary, i, nameLength));
                        i += nameLength + 36;
                    } while (i <= binary.Length - 128);

                    Console.WriteLine("找到{0}个短信文件夹。", ids.Count);
                    for (int j = 0; j < ids.Count; j++)
                        Console.WriteLine("  {0}:{1}", names[j], ids[j]);
                }
                else
                    Console.WriteLine("没有找到短信文件夹的开始标识！");

                m_Inited = true;
            }
        }
    }
}
